package com.io;

import java.io.*;

/**
 * @author weijingchen
 * @DateTime 2025-06-17
 */
public class CopyFiles {
    public static void main(String[] args) throws IOException {


        File srcPath = new File("D:\\test\\test.txt");
        File destPath = new File("D:\\test\\test1.txt");
        copyFile(srcPath, destPath);
    }

    /**
     * 文件夹拷贝
     *
     * @param srcPath
     * @param destPath
     * @throws IOException
     */

    public static void copyFile(File srcPath, File destPath) throws IOException {
        destPath.mkdir();
        File[] files = srcPath.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    FileInputStream fis = new FileInputStream(file);
                    FileOutputStream fos = new FileOutputStream(new File(destPath, file.getName()));
                    try (fis; fos) {
                        byte[] bytes = new byte[1024];
                        int len;
                        while ((len = fis.read(bytes)) != -1) {
                            fos.write(bytes, 0, len);
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    copyFile(file, new File(destPath, file.getName()));
                }
            }
        }
    }


    /**
     * 加密
     */
    public static void encryptFile(File srcPath, File destPath) throws IOException {
        FileInputStream fis = new FileInputStream(srcPath);
        FileOutputStream fos = new FileOutputStream(destPath);
        try (fis; fos) {
            int b;
            while ((b = fis.read()) != -1){
                fos.write(b ^ 5);
            }
        }
    }

}
